package com.hongxun.design.utils.fastjson;

import com.alibaba.fastjson.JSON;
import com.hongxun.design.MyApp;
import com.hongxun.design.bean.Exploration;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.MyLog;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zerdoor_pc .
 * author:dc
 * 2016/10/20.
 */
public class MergeUtil {
    public static void ExplorationList(String newpath, String oldPath, String rootnewPath, String rootoldPath) {


//        try {
//            if (!nFile.exists()) {
//                return;
//            } else if (!oFile.exists()) {
//                oFile.createNewFile();
//                MyApp.File.copyFile( nFile, oldPath);
////                MyApp.File.copyFolder(newpath, rootoldPath);//复制
//                return;
//            }
//        }catch (Exception e){
//            return;
//        }

        String s = "";
        String newjson = MyApp.File.Read(newpath);// 传来的数据
        String oldjson = MyApp.File.Read(oldPath);//缓存数据
        List<Exploration> newlist = FastJsonUtil.JsonToGetExplorationList(newjson);//传来的
        List<Exploration> oldlist = FastJsonUtil.JsonToGetExplorationList(oldjson);//缓存
        List<Exploration> list = new ArrayList<>();
        if (newlist == null || newlist.size() == 0) {
          if (oldlist==null||oldlist.size()==0){
              s = "{\"explorationList\":" + "}";
          }else{
              s = "{\"explorationList\":" + JSON.toJSONString(oldlist) + "}";
          }
            //新文件夹不存在此目录不做处理，维持原样
//            MyApp.File.copyCoverFolder("new", "old");//覆盖复制
            return;
        }

        if (oldlist == null || oldlist.size() == 0) {

            if (newlist == null || newlist.size() == 0) {
                s = "{\"explorationList\":"  +"}";
            } else{
                s = "{\"explorationList\":" + JSON.toJSONString(newlist) + "}";
            }//缓存文件夹不存在此目录直接复制过去
//            MyApp.File.copyCoverFolder(newpath, oldPath);//覆盖复制

            for (Exploration n : newlist) {
                copeToPath(rootnewPath, rootoldPath, n.getUuid());
            }

        } else {
            list.addAll(oldlist);

            for (int i = 0; i < newlist.size(); i++) {
                Exploration n = newlist.get(i);
                boolean flag = false;
                for (int j = 0; j < oldlist.size(); j++) {
                    Exploration o = oldlist.get(j);
                    if (n.getUuid().equals(o.getUuid())) {//uuid相同
                        flag = true;
                        if (o.getExploration_id() == null || o.getExploration_id().length() == 0 || o.getExploration_id().equals(MString.code_zero)) {
                            //有此项(覆盖)
                            list.remove(o);
                            list.add(n);
                            copeToPath(rootnewPath, rootoldPath, n.getUuid());
                            break;
                        }
                    }

                }

                if (!flag) {
                    //没有此项（覆盖）
                    list.add(n);
                    copeToPath(rootnewPath, rootoldPath, n.getUuid());
                }

            }
            s = "{\"explorationList\":" + JSON.toJSONString(oldlist) + "}";
        }
        //存储点位列表到oldPath
        try {
            MyApp.File.saveToSDCard(oldPath, s);
            MyLog.L("保存点位列表成功！");
        } catch (Exception e) {
            MyLog.L("保存点位列表异常：" + e.toString());
        }
    }

    /**
     * 遍历文件夹覆盖：
     * 1、判断接收的文件夹文件名字是否存在相同的uuid
     * 2、存在相同的uuid则覆盖
     **/
    private static void copeToPath(String rootnewPath, String rootoldPath, String uuid) {

        MyApp.File.copyCoverFolder(rootnewPath, rootoldPath, uuid);
    }

    public static boolean checkid(String id) {
        return !(id == null || id.length() == 0 || id.equals(MString.code_zero));
    }

    public static void TransformerList(String newpath, String oldPath) {
        String newjson = MyApp.File.Read(oldPath);// 传来的数据
        String oldjson = MyApp.File.Read(newpath);//缓存数据
        FastJsonUtil.JsonToGetExplorationList(newjson);//传来的
        FastJsonUtil.JsonToGetExplorationList(oldjson);//缓存
        String s = "";
        if (s != null && s.length() != 0) {
            try {
                MyApp.File.saveToSDCard(oldjson, "");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void ExplorePointInfoPhoto(String newpath, String oldPath) {
        String newjson = MyApp.File.Read(oldPath);// 传来的数据
        String oldjson = MyApp.File.Read(newpath);//缓存数据
        FastJsonUtil.JsonToGetExplorationList(newjson);//传来的
        FastJsonUtil.JsonToGetExplorationList(oldjson);//缓存
        String s = "";
        if (s != null && s.length() != 0) {
            try {
                MyApp.File.saveToSDCard(oldjson, "");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void OnChannelList(String newpath, String oldPath) {
        String newjson = MyApp.File.Read(oldPath);// 传来的数据
        String oldjson = MyApp.File.Read(newpath);//缓存数据
        FastJsonUtil.JsonToGetExplorationList(newjson);//传来的
        FastJsonUtil.JsonToGetExplorationList(oldjson);//缓存
        String s = "";
        if (s != null && s.length() != 0) {
            try {
                MyApp.File.saveToSDCard(oldjson, "");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
